﻿using System;

namespace EmbeddedSharp.Core.Function.Flow {
    public class CaseNode : FuncNode {

        public CaseNode()
            : base() {
            base.Value = "case";
        }

        public override object Evaluate() {
            int count = Parameters.Count;
            object condition = Parameters[count - 1].Evaluate();

            for (int index = count - 2; index > 0; index = index - 2) {
                object value = Parameters[index - 1].Evaluate();
                if (value == condition) {
                    return Parameters[index].Evaluate();
                }
            }

            return null;
        }
    }
}
